<template>
  <div>
    <section>
      <div class="box">
        <div class="container">
          <div class="form">
            <h2>注册</h2>
            <el-form
              ref="registerForm"
              :model="registerForm"
              :rules="registerRules"
            >
              <el-form-item class="inputBox" prop="username">
                <el-input
                  type="text"
                  placeholder="请输入账号"
                  v-model="registerForm.username"
                />
              </el-form-item>
              <el-form-item class="inputBox" prop="password">
                <el-input
                  type="password"
                  placeholder="请输入密码"
                  v-model="registerForm.password"
                />
              </el-form-item>
              <el-form-item class="inputBox" prop="email">
                <el-input
                  type="text"
                  placeholder="请输入邮箱"
                  v-model="registerForm.email"
                />
              </el-form-item>
              <el-form-item prop="code" class="inputBox">
                <div class="code">
                  <el-input
                    type="text"
                    placeholder="请输入验证码"
                    v-model="registerForm.code"
                  />
                  <el-button
                    class="code_btn"
                    @click="handleCode()"
                    :disabled="getCodeBtnDisable"
                  >
                    <span>{{ codeBtnWord }}</span>
                  </el-button>
                </div>
              </el-form-item>
              <div class="btn-box">
                <el-form-item>
                  <el-button
                    class="register_btn"
                    @click="handleregister('registerForm')"
                  >
                    <span>注 册</span>
                  </el-button>
                </el-form-item>
                <el-form-item>
                  <el-button class="register_btn" @click="toLogin">
                    <span>登 录</span>
                  </el-button>
                </el-form-item>
              </div>
            </el-form>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>
  
  <script>
import api from "@api/index";
import { setToken, setUserInfo } from "@/utils/auth";
// import axios from "axios";
export default {
  data() {
    return {
      codeBtnWord: "获取验证码", // 获取验证码按钮文字
      waitTime: 61, // 获取验证码按钮失效时间
      getCodeBtnDisable: false,
      registerForm: {
        username: "",
        password: "",
        email: "",
        code: "",
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "账号不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
      },
    };
  },
  methods: {
    handleCode() {
      if (this.registerForm.username && this.registerForm.email) {
        this.phoneTimer();
        var data = {
          username: this.registerForm.username,
          email: this.registerForm.email,
        };
        api.getCode(data).then(
          (res) => {
            console.log(res);
          },
          (err) => {
            console.log(err);
          }
        );
      } else {
        this.$notify({
          title: "用户名或邮箱不能为空！",
          type: "warning",
          //存在时间
          duration: 6000,
        });
      }
    },
    // 倒计时计时器方法
    phoneTimer() {
      const that = this;
      that.waitTime--;
      that.getCodeBtnDisable = true;
      this.codeBtnWord = `${this.waitTime}s 后重新获取`;
      console.log("获取时间的值2：" + this.waitTime);
      const timer = setInterval(function () {
        if (that.waitTime > 1) {
          that.waitTime--;
          that.codeBtnWord = `${that.waitTime}s 后重新获取`;
        } else {
          clearInterval(timer);
          that.codeBtnWord = "获取验证码";
          that.getCodeBtnDisable = false;
          that.waitTime = 61;
        }
      }, 1000);
    },
    handleregister(registerForm) {
      this.$refs[registerForm].validate((valid) => {
        if (valid) {
          var params = {
            username: this.registerForm.username,
            code: this.registerForm.code,
          };
          api.verifyCode(params).then(
            (res) => {
              console.log(res);
              if (res.code === 200) {
                var data = {
                  username: this.registerForm.username,
                  password: this.registerForm.password,
                  email: this.registerForm.email,
                };
                api.register(data).then(
                  (res) => {
                    console.log(res);
                    if (res.code === 200) {
                      this.$notify({
                        title: "注册成功！",
                        type: "success",
                        duration: 6000,
                      });
                      this.$router.push({ path: "/Login" });
                    } else {
                      this.$notify({
                        title: res.msg,
                        type: "error",
                        duration: 6000,
                      });
                    }
                  },
                  (err) => {
                    console.log(err);
                  }
                );
              } else {
                this.$notify({
                  title: res.msg,
                  type: "error",
                  duration: 6000,
                });
              }
            },
            (err) => {
              console.log(err);
            }
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    toLogin() {
      this.$router.push({ path: "/Login" });
    },
  },
};
</script>
  
  <style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* 使用flex布局，让内容垂直和水平居中 */

section {
  min-height: 100vh;
  background-image: url(../assets/images/bg3.jpg);
  background-size: cover;
  background-position: center center;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 登录框样式 */

.container {
  width: 440px;
  min-height: 400px;
  background: rgba(255, 255, 255, 0.1);
  display: flex;
  justify-content: center;
  align-items: center;
  backdrop-filter: blur(5px);
  box-shadow: 0 25px 45px rgba(0, 0, 0, 0.1);
  border: 0;
}

.form {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 50px;
}

/* 登录标题样式 */

.form h2 {
  position: relative;
  color: #fff;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: 5px;
  margin-bottom: 30px;
  cursor: pointer;
  text-align: center;
}

.form .inputBox {
  width: 100%;
  margin-top: 20px;
}

/* 输入框样式 */

/deep/.form .inputBox input {
  width: 100%;
  padding: 20px;
  background: rgba(255, 255, 255, 0.2);
  outline: none;
  border: none;
  border-radius: 30px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-right: 1px solid rgba(255, 255, 255, 0.2);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  font-size: 16px;
  letter-spacing: 1px;
  color: #fff;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
}

/deep/.form .inputBox input::placeholder {
  color: #fff;
}

.btn-box{
  margin-top: 40px;
}

.register_btn {
  background: #fff;
  color: #666;
  /* padding: 6px; */
  width: 70%;
  height: 40px;
  border-radius: 30px;
  font-size: 20px;
  font-family: "KaiTi";
  font-weight: 600;
  margin: 2px 15%;
}
.register_btn:hover {
  background-color: rgba(255, 255, 255, 0.5);
  animation: register_mation 0.5s;
  color: #666;
}
.code {
  display: flex;
  flex-direction: row;
}
/deep/.form .code input {
  width: 90%;
  display: inherit;
}
.code_btn {
  background: #fff;
  color: #666;
  /* padding: 6px; */
  width: 70%;
  height: 40px;
  border-radius: 30px;
  font-size: 20px;
  font-family: "KaiTi";
  font-weight: 600;
  padding: 0 10px;
}
.code_btn:hover {
  background-color: rgba(255, 255, 255, 0.5);
  animation: register_mation 0.5s;
  color: #666;
}
</style>